//
//  PersonTableViewController.m
//  Ecrm
//
//  Created by ZHC on 16/3/15.
//  Copyright © 2016年 ZHC. All rights reserved.
//

#import "PersonTableViewController.h"
#import "PerHeaderView.h"
#import "PerFristTableViewCell.h"
#import "PerHeaderModel.h"
#import "PerFristBodyModel.h"
#import <AFNetworking.h>
#import "HCHTTPManager.h"
@interface PersonTableViewController ()
{
    PerHeaderModel *_headerModel;
    NSMutableArray *_cellModelArray;
}
@end

@implementation PersonTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
   

}

/**
 *  返回头试图
 */
-(UIView *)getHeaderView{
    PerHeaderView *header = [[PerHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenWidth*0.65)];
    header.model = _headerModel;
    
    return header;
}


/**
 *  加载数据
 */
-(void)loadData{
    HCHTTPManager *mgr = [HCHTTPManager httpManager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"Appkey"] = @"5A397CFBCC96E4A7A715F9FB8B3051B3";
    parameters[@"token"] = @"F57469913310013B66D9E9DD25CF4FB6";
    parameters[@"userid"] = @"411";
    parameters[@"eid"] = @"a360";
    parameters[@"type"] = @"2";
    [mgr POST:@"http://jiekou.aasaas.net/1.2.0/Homepage.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        HCLog(@"responseObject --------------%@",responseObject);
        if ([responseObject[@"status"] isEqualToString:@"1"]) {
            
            _headerModel = [PerHeaderModel mj_objectWithKeyValues:responseObject[@"data"]];
            
            //根据模型布局
            [self setCellModel];
            self.tableView.tableHeaderView = [self getHeaderView];
            [self.tableView reloadData];
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        HCLog(@"error -----------%@",error);
        
        
    }];
}


/**
 *  设置cell数据数组
 */
-(void)setCellModel{
    _cellModelArray = [NSMutableArray array];
    
    [self setCellModelIcon:@"icon_case" Name:@"手机:" AndValue:_headerModel.contacttel];
    [self setCellModelIcon:@"icon_case" Name:@"邮箱:" AndValue:_headerModel.mail];
    [self setCellModelIcon:@"icon_case" Name:@"Q Q:" AndValue:_headerModel.QQ];
    [self setCellModelIcon:@"icon_case" Name:@"职位:" AndValue:_headerModel.zhiwei];
    [self setCellModelIcon:@"icon_case" Name:@"公司:" AndValue:_headerModel.ename];
    [self setCellModelIcon:@"icon_case" Name:@"地址:" AndValue:_headerModel.address];
    
}

/**
 *  设置cell模型
 */
-(void)setCellModelIcon:(NSString *)icon Name:(NSString *)name AndValue:(NSString *)value{
    PerFristBodyModel *model = [[PerFristBodyModel alloc]init];
    model.icon = icon;
    model.name = name;
    model.value = value;
    
    [_cellModelArray addObject:model];
}

#pragma mark - tableView代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PerFristTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"perCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PerFristTableViewCell" owner:nil options:nil]lastObject];
    }
    
    if (indexPath.section<2) {
        
        if (_cellModelArray.count >0) {
            
            cell.model = _cellModelArray[indexPath.row + (indexPath.section * 3)];
        }
    }
    
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *header = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
    header.font = [UIFont systemFontOfSize:14];
    switch (section) {
        case 0:
            header.text = @"   联系人信息";
            break;
            
        case 1:
            header.text = @"   详细信息";
            break;
            
        case 2:
            header.text = @"   二维码";
            break;
            
        default:
            break;
    }
    
    
    return header;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}


@end
